package com.rongxun.common.base.mvvm;


import android.content.Intent;
import android.os.Bundle;

import com.rongxun.api.utils.FlyLog;
import com.rongxun.common.R;
import com.rongxun.common.base.mvvm.view.IBaseView;
import com.rongxun.common.base.mvvm.viewmodel.BaseViewModel;
import com.rongxun.common.utils.ActivityUtils;

import java.util.Map;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;


/**
 * 创建者     forza
 * 创建时间   2019/11/29 18:09
 * 描述
 */
public abstract class BaseMvvmActivity<V extends ViewDataBinding, VM extends BaseViewModel> extends BaseRxActivity implements IBaseView {
    protected V   mBinding;
    protected VM  mViewModel;
    private   int viewModelId;


    protected void initContentView() {
        initViewDataBinding();
        initBaseViewObservable();
        initViewObservable();

        if (getIntent() != null) {
            mViewModel.setIntent(getIntent());
        }

    }


    private void initViewDataBinding() {
        mBinding = DataBindingUtil.setContentView(this, onBindLayout());
        viewModelId = onBindVariableId();
        mViewModel = createViewModel();
        if (mBinding != null) {
            mBinding.setVariable(viewModelId, mViewModel);
        }
        getLifecycle().addObserver(mViewModel);
    }

    protected abstract int onBindLayout();

    protected abstract int onBindVariableId();


    private VM createViewModel() {
        return ViewModelProviders.of(this, onBindViewModelFactory()).get(onBindViewModel());
    }

    protected abstract ViewModelProvider.Factory onBindViewModelFactory();


    protected abstract Class<VM> onBindViewModel();


    private void initBaseViewObservable() {
        mViewModel.getUIChangeLiveData().getShowInitLoadViewEvent().observe(this, (Observer<Boolean>) show -> showInitLoadView(show));
        mViewModel.getUIChangeLiveData().getShowTransLoadingViewEvent().observe(this, (Observer<Boolean>) show -> {
            FlyLog.d("view postShowTransLoadingViewEvent start...");
            showTransLoadingView(show);
        });
        mViewModel.getUIChangeLiveData().getShowNoDataViewEvent().observe(this, (Observer<Boolean>) show -> showNoDataView(show));
        mViewModel.getUIChangeLiveData().getShowNetWorkErrViewEvent().observe(this, (Observer<Boolean>) show -> showNetWorkErrView(show));
        mViewModel.getUIChangeLiveData().getStartActivityEvent().observe(this, (Observer<Map<String, Object>>) params -> {


            Class<?> clz = (Class<?>) params.get(BaseViewModel.ParameterField.CLASS);
            Bundle bundle = (Bundle) params.get(BaseViewModel.ParameterField.BUNDLE);
            startActivity(clz, bundle);


        });
        mViewModel.getUIChangeLiveData().getFinishActivityEvent().observe(this, (Observer<Void>) v -> finish());
        mViewModel.getUIChangeLiveData().getOnBackPressedEvent().observe(this, (Observer<Void>) v -> onBackPressed());
    }

    private void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent(this, clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        ActivityUtils.startActivityWithAnimotion(this, intent);
    }

    protected abstract void initViewObservable();


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBinding != null) {
            mBinding.unbind();
        }
    }

    protected boolean existActivityWithAnimation = true;

    public void onBackPressed() {
        super.onBackPressed();
        if (existActivityWithAnimation) {
            overridePendingTransition(R.anim.slide_in_left, R.anim.side_out_right);
        }
    }


}
